package com.piper.consumer.controller;

import com.piper.consumer.entity.AccountDto;
import com.piper.consumer.service.TestService;
import com.piper.core.RequestData;
import com.piper.core.ResultData;
import com.piper.model.User;
import com.piper.consumer.consumer.UserConsumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author piper
 * @date 2019/1/15 9:26
 */
@RestController
public class TestController {

    @Resource
    private UserConsumer userConsumer;

    @Resource
    private TestService testService;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping(path = "/test")
    public User test(@RequestParam("name") String name) {
        return userConsumer.test(name);
    }

    @GetMapping(path = "/retry")
    public String retry() {
        ResponseEntity<String> entity = restTemplate.getForEntity("http://provider/retry", String.class);
        return entity.getBody();
    }

    @RequestMapping("testHolder")
    public ResultData testHolder(RequestData request) {
        System.err.println(request.getData());
        return ResultData.success("请求成功");
    }

    @RequestMapping("testValid")
    public ResultData testValid(RequestData request) {
        AccountDto accountDto = request.parse(AccountDto.class);
        testService.testValid(accountDto);
        return ResultData.success();
    }

}
